package cn.edu.scau.cmi.ChenKaihui.client;

import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.ChenKaihui.abstractFactory.domainAbstractClass.Insurance;
import cn.edu.scau.cmi.ChenKaihui.abstractFactory.factory.AbstractFactory;
import cn.edu.scau.cmi.ChenKaihui.abstractFactory.factory.PICCFactory;
import cn.edu.scau.cmi.ChenKaihui.abstractFactory.factory.PingAnFactory;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class AbstractFactoryClient extends Application implements Initializable{
	
	 @FXML
	 public ChoiceBox<String> companyChoiceBox;
	 
	 @FXML
	 public ChoiceBox<String> insuranceChoiceBox;
	 
	 @FXML
	 public TextArea textArea;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		ObservableList<String> companyOptions = FXCollections.observableArrayList("PICC", "PingAn");
        companyChoiceBox.setValue("PICC");
        companyChoiceBox.setItems(companyOptions);
        ObservableList<String> insuranceOptions = FXCollections.observableArrayList("身体受伤", "汽车损坏", "人员伤亡", "多种事故");
        insuranceChoiceBox.setValue("身体受伤");
        insuranceChoiceBox.setItems(insuranceOptions);
	}

	public void displayMessage() {
		
		String companyCB = companyChoiceBox.getValue();
		AbstractFactory abstractFactory = null;
		switch (companyCB) {
		case "PICC": 
			abstractFactory = new PICCFactory();
			break;
		case "PingAn": 
			abstractFactory = new PingAnFactory();
			break;
		}
		if(abstractFactory == null) {
			return;
		}
		
		String insuranceCB = insuranceChoiceBox.getValue();
		Insurance insurance = null;
		switch(insuranceCB) {
		case "身体受伤":
			insurance = abstractFactory.isPhysicalInsurance();
		    break;
		case "汽车损坏":
			insurance = abstractFactory.isVehicleInsurance();
		    break;
		case "人员伤亡":
			insurance = abstractFactory.isCasualtyInsurance();
		    break;
		case "多种事故":
			insurance = abstractFactory.isAccidentInsurance();
		    break;
		}
		if(insurance != null) {
			textArea.setText(insurance.show());
		}
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		Parent root = FXMLLoader.load(getClass().getResource("../UI/abstractFactoryClientUI.fxml"));
        primaryStage.setTitle("抽象工厂模式");
        primaryStage.setScene(new Scene(root, 800, 800));
        primaryStage.setResizable(false);
        primaryStage.show();		
	}
	
	public static void main(String[] args) {
		launch(args);
	}

}
